//
//  TweetsViewController.m
//  oschina_song
//
//  Created by song on 16/1/26.
//  Copyright © 2016年 song. All rights reserved.
//

#import "TweetsViewController.h"
#import "OSCTweets.h"
#import "TweetsCell.h"

static NSString *kTweetCellID =@"KTWEETCELLID";
@interface TweetsViewController ()

@property(nonatomic ,assign)TweetsType type;

@end

@implementation TweetsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _textView = [[UITextView alloc] initWithFrame:CGRectZero];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(id)initWithTweetsType:(TweetsType)type
{
    self = [super init];
    if(self)
    {
        _type = type;
        self.objclass = [OSCTweets class];
        
        self.generateURL = ^NSString *(NSUInteger page){
            return [NSString stringWithFormat:@"%@%@?uid=%ll&pageIndex=%lu&%@&clientType=android",OSCAPI_PREFIX,OSCAPI_TWEETS_LIST,0,(unsigned long)page ,OSCAPI_SUFFIX];
        };
        
        
        [self.tableView registerClass:[TweetsCell class] forCellReuseIdentifier:kTweetCellID];
    }
    
    return  self;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OSCTweets *tweet = self.objects[indexPath.row];
    if(tweet.cellHeight) {return tweet.cellHeight;};
    
    [self.label setText:tweet.author];
    CGFloat height = [self.label sizeThatFits:CGSizeMake(tableView.frame.size.width -60, MAXFLOAT)].height;
    
    [self.textView setText:tweet.body];
    height += [self.textView sizeThatFits:CGSizeMake(tableView.frame.size.width-60 ,MAXFLOAT)].height;
    
    tweet.cellHeight = height;
    return tweet.cellHeight;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    OSCTweets *tweets = self.objects[indexPath.row];
    
    TweetsCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kTweetCellID forIndexPath:indexPath];
    
    [cell setContentWithTweet:tweets];
    
    return cell;
}

-(NSArray *)parseXML:(ONOXMLDocument *)xml
{
    return [[xml.rootElement firstChildWithTag:@"tweets"] childrenWithTag:@"tweet"];

}

@end
